﻿using AutoMapper;
using MediatR;
using SmartApplication.Api.Applient.Command.Source;
using SmartApplication.ApiCode;
using SmartApplication.Domain.Entity;
using SmartApplication.Infrastructure.Instrument;
using System;
using Yitter.IdGenerator;

namespace SmartApplication.Api.Applient.CommandHandler.Source
{
    public class UpdateSourceHandler : IRequestHandler<UpdateSourceCommand, ApiResult<int>>
    {
        public UpdateSourceHandler(IBaseRepository<SourceModel> sourceRepository,IMapper mapper)
        {
            SourceRepository = sourceRepository;
            Mapper = mapper;
        }

        public IBaseRepository<SourceModel> SourceRepository { get; }
        public IMapper Mapper { get; }

        /// <summary>
        /// 修改溯源基础数据
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<ApiResult<int>> Handle(UpdateSourceCommand request, CancellationToken cancellationToken)
        {
            var res=new ApiResult<int>();

            var souce = await SourceRepository.GetValueAsync(request.Id);

            souce.AccountName = request.AccountName;
            souce.UpdateTime = DateTime.Now;
            souce.UpdateName = "张三";
            souce.Enterprise = request.Enterprise;
            souce.Expiration = request.Expiration;
            souce.Pic = request.Pic;
            souce.ShopName = request.ShopName;
            souce.ShopPic = request.ShopPic;
                 

            var ss=await SourceRepository.UpdateAsync(souce);
            if (ss>0)
            {
                res.Code = EnumCode.Success;
                res.Message = "修改溯源基础数据成功";
                res.Data = ss;
                return res;
            }
            else
            {
                res.Code = EnumCode.Fail;
                res.Message = "修改溯源基础数据失败";
                res.Data = ss;
                return res;
            }
        }
    }
}
